package com.signcomplex.nettyandroid;

import android.os.Handler;
import android.os.Message;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class TimeClientHandler1 extends ChannelInboundHandlerAdapter {

    static final String ECHO_REQ = "Hi, L有545ili8玉nfeng. Welcome to Netty.$";

    private ChannelHandlerContext channelHandlerContext = null;
    private boolean isConnect = false;

    public boolean isConnect() {
        return isConnect;
    }

    private static class SingleTonHolder {
        public final static TimeClientHandler1 instance = new TimeClientHandler1();
    }

    public static TimeClientHandler1 getInstance() {
        // 直接获取内部类的变量
        return SingleTonHolder.instance;
    }

    public ChannelHandlerContext getChannelHandlerContext() {
        return channelHandlerContext;
    }

    private Handler handler;

    /**
     * Creates a client-side handler.
     */
    public TimeClientHandler1() {
        LogUtil.i("TimeClientHandler-->构造方法");
    }

    public void init(Handler handler) {
        LogUtil.i("TimeClientHandler-->init");
        this.handler = handler;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // TODO Auto-generated method stub
        LogUtil.i("TimeClientHandler-->channelActive");

        isConnect = true;
        channelHandlerContext = ctx;

//        for (int i = 0; i < 10; i++) {
//            ctx.writeAndFlush(Unpooled.copiedBuffer(ECHO_REQ.getBytes()));
//        }

        byte[] req = "QUERY TIME ORDER".getBytes();
//        byte[] req = new byte[10];
//        req[0] = 0x12;//18
//        req[1] = 54;//54
//        req[2] = (byte)'d';//100
//        req[3] = 5;//5
//        req[4] = (byte)'发';//-47
//        req[5] = (byte)'D';//68

        ByteBuf firstMessage = Unpooled.buffer(req.length);
        firstMessage.writeBytes(req);
        ctx.writeAndFlush(firstMessage);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // TODO Auto-generated method stub
        LogUtil.i("TimeClientHandler-->channelRead");

        ByteBuf buf = (ByteBuf) msg;
        byte[] req = new byte[buf.readableBytes()];
        buf.readBytes(req);

//        int len = req.length;
//        for (int i = 0; i < len; i++) {
//            logger.warning(req[i] + "");
//        }

        String body = new String(req, "UTF-8");
        LogUtil.i("client channelRead-->" + body);

        LogUtil.i("handler-->" + handler);

        if (handler != null) {
            Message message = new Message();
            message.what = 12;
            message.obj = "wo shi handler";
            handler.sendMessage(message);
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        LogUtil.i("TimeClientHandler-->channelReadComplete");

        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // TODO Auto-generated method stub
        LogUtil.i("TimeClientHandler-->exceptionCaught");

        // 释放资源
        LogUtil.i("Unexpected exception from downstream : " + cause.getMessage());
        ctx.close();
    }

}
